<?php
namespace app\wechat\controller;

use Jssdk\JSSDK;

class RMember extends Common
{
    public function index()
    {

        /*test
        $param["order_id"] = 422;
        $data = call_interface('repair_order_product_server_list',$param);
        print_r($data);
        exit();*/

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $member_id = session('member_id');
        // 检查是否有维修店权限
        $repair = db('repair')->where('member_id', $member_id)->find();
        $this->assign('repair', $repair);
        if (!$repair) {
            // 没有申请 转入申请提示页面
            return view('r_member/audit');
        } else if ($repair['audit_status'] != 4) {
            // 审核状态页面
            $this->redirect('r_certification/audit_status', ['status' => $repair['audit_status']]);
        }

        // 获取维修店首页数据
        $param = [
            'member_id' => session('member_id'),
            'terrace' => 'weixin'
        ];
        $data = call_interface('repair_index_data',$param);
        if($data["code"]==200){
            $this->assign('data', $data["data"]);
        }else{
            return $data["msg"];
        }

        return view();
    }

    public function index_map()
    {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $member_id = session('member_id');
        // 检查是否有维修店权限
        $repair = db('repair')->where('member_id', $member_id)->find();
        $this->assign('repair', $repair);
        if (!$repair) {
            // 没有申请 转入申请提示页面
            return view('r_member/audit');
        } else if ($repair['audit_status'] != 4) {
            // 审核状态页面
            $this->redirect('r_certification/audit_status', ['status' => $repair['audit_status']]);
        }

        // 获取维修店首页数据
        $param = [
            'member_id' => session('member_id'),
            'terrace' => 'weixin'
        ];
        $data = call_interface('repair_index_data',$param);
        if($data["code"]==200){
            $this->assign('data', $data["data"]);
        }else{
            return $data["msg"];
        }

        return view();
    }

    // 获取订单列表
    public function get_orders() {

        if (request()->isAjax()) {
            // 获取订单列表接口
            $order_id = intval(input('param.order_id',0));
            $repair_id = intval(input('param.repair_id',0));
            $param = [
                'member_id' => session('member_id')
            ];
            $data = call_interface('repair_get_orders',$param);
            return $data;
        }
        $this->redirect('base/error');

    }

    // 获取订单列表(地图范围内)
    public function get_orders_map() {

        if (request()->isAjax()) {
            // 获取订单列表接口(地图范围内)
            //$get_type = intval(input('param.type',0));
            $get_lng = floatval(input('param.lng',0));
            $get_lat = floatval(input('param.lat',0));
            $get_zoom = floatval(input('param.zoom',0));
            $param = [
                'member_id' => session('member_id')
                ,'lng' => $get_lng
                ,'lat' => $get_lat
                ,'zoom' => $get_zoom
            ];
            $data = call_interface('repair_get_orders_map',$param);
            return $data;
        }
        $this->redirect('base/error');

    }

    // 订单信息
    public function order_info() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){
            session('member_id',@intval($_COOKIE["PJ"]));
            $signPackage = [
                'appId' => '',
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => ''
            ];
        }else{
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->GetSignPackage();
        }
        $this->assign('signPackage', $signPackage);

        $where = " member_id='".session('member_id')."' ";
        $repair = db('repair')->field('id,repair_name,lat,lon')->where($where)->find();
        $this->assign('repair', $repair);

        $order_id = intval(input('param.id',0));
        $param = [
            'order_id' => $order_id
        ];
        $order = call_interface('repair_order_info',$param);
        if($order["code"]==200){
            $this->assign('order', $order["data"]);
        }else{
            $this->redirect('base/error');
        }

        return view();
    }

    // 接订单
    public function order_get() {


        if (request()->isAjax()) {

            $order_id = intval(input('param.order_id',0));
            $repair_id = intval(input('param.repair_id',0));

            $order_info = db('order_server')->where("id",$order_id)->find();
            if($order_info["status"]==1){
                // 维修店抢单接口
                $param = [
                    'order_id' => $order_id,
                    'repair_id' => $repair_id
                ];
                $data = call_interface('order_server_receiving',$param);
                return json($data);
            }else{
                if($repair_id==$order_info['repair_id']){
                    $tmpStr = "您已经接受了这个订单";
                }else{
                    $tmpStr = "已经被别的维修店接受";
                }
                return json(['data'=>$order_info['repair_id'], 'code'=>0, 'msg'=> $tmpStr.'('.get_server_order_status($order_info["status"]).')' ]);
            }

        }
        $this->redirect('base/error');

    }

    // 订单推送
    public function order_push() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $where = " member_id='".session('member_id')."' ";
        $repair = db('repair')->where($where)->find();
        if(!$repair){
            $this->redirect('base/error');
        }

        $order_id = intval(input('param.id',0));
        $param = [
            'order_id' => $order_id
        ];
        $order = call_interface('repair_order_info',$param);
        if($order["code"]==200){
            //print_r($order["data"]);exit();
            $this->assign('order', $order["data"]);
        }else{
            $this->redirect('base/error');
        }

        return view();
    }


    // add_product
    public function add_product() {

        if (request()->isAjax()) {
            $repair_id = input('param.repair_id','');
            $name = input('param.name','');
            $price = floatval(input('param.price',0));
            $img1 = input('param.img1','');
            $img2 = input('param.img2','');
            $img3 = input('param.img3','');
            $img4 = input('param.img4','');
            $img5 = input('param.img5','');
            $service_expires = input('param.service_expires','');

            $where = " repair_id='".$repair_id."' and name='".$name."' ";
            $goods = db('repair_product')->where($where)->find();
            if($goods){
                $data = [
                    "code" => 200,
                    "data" => [
                        "id" => $goods["id"],
                        "img" => $goods["img1"]
                    ],
                    "msg" => "SUCCESS"
                ];
            }else {
                $param = [
                    'repair_id' => $repair_id,
                    'name' => $name,
                    'price' => $price,
                    'img1' => $img1,
                    'img2' => $img2,
                    'img3' => $img3,
                    'img4' => $img4,
                    'img5' => $img5,
                    'service_expires' => $service_expires
                ];
                $data = call_interface('repair_add_product',$param);
            }
            return $data;
        }
        $this->redirect('base/error');
    }

    // 商品搜索
    public function goods_so() {

        if (request()->isAjax()) {

            $repair_id = intval(input('param.repair_id',0));
            $keyword = input('param.keyword','');
            $param = [
                'repair_id' => $repair_id,
                'page_no' => 0,
                'page_size' => 50,
                'keyword' => $keyword
            ];
            $data = call_interface('repair_so_product',$param);
            return $data;

        }
        $this->redirect('base/error');
        return view();
    }

    // 向订单添加商品或服务
    public function order_product_server_add() {

        if (request()->isAjax()) {

            $order_id = input('param.order_id','0');
            $repair_id = input('param.repair_id','0');
            $product_id = input('param.product_id','0');
            $product_name = input('param.product_name','');
            $product_num = input('param.product_num','1');
            $product_price = input('param.product_price','0');
            //$product_attr_id = input('param.product_attr_id','0');
            $product_image = input('param.product_image','');
            $type = input('param.type','0');
            $msg = input('param.msg','');
            $server_time = input('param.server_time','0');
            $param = [
                "order_id" => $order_id,
                "repair_id" => $repair_id,
                "product_id" => $product_id,
                "product_name" => $product_name,
                "product_num" => $product_num,
                "product_price" => $product_price,
                //"product_attr_id" => $product_attr_id,
                "product_image" => $product_image,
                "type" => $type,
                "msg" => $msg,
                "server_time" => $server_time
            ];
            $data = call_interface('repair_order_product_server_add',$param);
            return $data;

        }
        $this->redirect('base/error');

    }

    // 修改推送订单的商品或服务
    public function order_product_server_update() {

        if (request()->isAjax()) {

            $id = input('param.id','0');
            $order_id = input('param.order_id','0');
            $repair_id = input('param.repair_id','0');
            $product_id = input('param.product_id','0');
            $product_name = input('param.product_name','');
            $product_num = input('param.product_num','0');
            $product_price = input('param.product_price','0');
            //$product_attr_id = input('param.product_attr_id','0');
            $product_image = input('param.product_image','');
            $type = input('param.type','0');
            $msg = input('param.msg','');
            $server_time = input('param.server_time','0');
            $img2 = input('param.img2','');
            $img3 = input('param.img3','');
            $img4 = input('param.img4','');
            $img5 = input('param.img5','');

            $param = [
                "id" => $id,
                "order_id" => $order_id,
                "repair_id" => $repair_id,
                "product_id" => $product_id,
                "product_name" => $product_name,
                "product_num" => $product_num,
                "product_price" => $product_price,
                //"product_attr_id" => $product_attr_id,
                "product_image" => $product_image,
                "type" => $type,
                "msg" => $msg,
                "img2" => $img2,
                "img3" => $img3,
                "img4" => $img4,
                "img5" => $img5,
                "server_time" => $server_time
            ];
            $data = call_interface('repair_order_product_server_update',$param);
            return $data;

        }
        $this->redirect('base/error');

    }

    // 删除订单商品或服务
    public function order_product_server_delete() {

        if (request()->isAjax()) {

            $id = input('param.id',0);
            $order_id = input('param.order_id',0);
            $param = [
                "id" => $id,
                "order_id" => $order_id
            ];
            $data = call_interface('repair_order_product_server_delete',$param);
            return $data;

        }
        $this->redirect('base/error');

    }

    // 确认推送订单
    public function repair_order_push() {

        if (request()->isAjax()) {

            $order_id = input('param.order_id','0');
            $repair_id = input('param.repair_id','0');
            $param = [
                "order_id" => $order_id,
                "repair_id" => $repair_id
            ];
            $data = call_interface('repair_order_push',$param);
            return $data;

        }
        $this->redirect('base/error');

    }

    // 实名认证
    public function certification() {
        return view();
    }

    // 搜索维修店历史推送订单的用户接口
    public function order_push_so() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $where = " member_id='".session('member_id')."' ";
        $repair = db('repair')->where($where)->find();
        if(!$repair){
            $this->redirect('base/error');
        }

        $param["repair_id"] = $repair["id"];
        $order = call_interface('repair_order_push_history',$param);
        $this->assign('order', $order["data"]);

        return view();
    }


    // 根据手机号码创建直接推送的订单
    public function order_push_so_next() {

        if (request()->isAjax()) {

            /*PJ非微信本地测试方法*/
            if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}
            $phone = input('param.phone',0);

            $where = " member_id='".session('member_id')."' ";
            $repair = db('repair')->where($where)->find();
            if(!$repair){
                return ['code'=>400029, 'msg'=> 'FAULT'];
            }
            $member = db('member')->field('phone')->where('id',$repair['member_id'])->find();
            if($member["phone"]==$phone){
                return ['code'=>400009, 'msg'=> 'FAULT'];
            }

            if(!$repair){
                $this->redirect('base/error');
            }else{
                $param["repair_id"] = $repair["id"];
                $param["phone"] = $phone;
                $param["source"] = "web";
                $order = call_interface('repair_phone_so_order',$param);
                return $order;
            }

        }
        $this->redirect('base/error');

    }


//================================================销售采购===============================================


    // 维修店销售采购首页
    public function deal_index() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $where = " member_id='".session('member_id')."' ";
        $repair = db('repair')->where($where)->find();
        if(!$repair){
            $this->redirect('base/error');
        }
        $this->assign('repair', $repair);

        $param["repair_id"] = $repair["id"];
        $param["page_no"] = 0;
        $param["page_size"] = 50;
        $order = call_interface('repair_deal_push_history',$param);
        //print_r($order);exit();
        $this->assign('order', $order["data"]);

        return view();
    }

    // 根据手机号码创建销售采购的推送订单
    public function deal_push_so_next() {

        if (request()->isAjax()) {

            /*PJ非微信本地测试方法*/
            if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

            $where = " member_id='".session('member_id')."' ";
            $repair = db('repair')->where($where)->find();

            if(!$repair){
                $this->redirect('base/error');
            }else{
                $param["repair_id"] = $repair["id"];
                $param["phone"] = input('param.phone',0);
                $param["source"] = "web";
                $order = call_interface('repair_phone_so_deal',$param);
                return $order;
            }

        }
        $this->redirect('base/error');

    }


    // 销售采购订单推送页面
    public function deal_push() {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $where = " member_id='".session('member_id')."' ";
        $repair = db('repair')->where($where)->find();
        if(!$repair){
            $this->redirect('base/error');
        }

        $order_id = intval(input('param.id',0));
        $param = [
            'order_id' => $order_id
        ];
        $order = call_interface('repair_deal_info',$param);
        //print_r($order);exit();
        if($order["code"]==200){
            //已添加的服务或产品
            $product_server = db('order_product_server_deal')->alias('ops')->field('ops.*,rp.img1,rp.img2,rp.img3,rp.img4,rp.img5')
                ->join('mob_repair_product rp', 'rp.id = ops.product_id', 'LEFT')
                ->where('ops.order_id',$param['order_id'])->order("ops.id desc")->select();
            $order["data"]["product_server"] = $product_server;

//            if($order["data"]["status"]==2){//如果是待付款状态,修改为议价状态并添加修改时间
//                $update = db("order_server_deal")->where('id',$order_id)->update(["status"=>11,"push_change_time"=>time()]);
//                if(!$update){
//                    $this->redirect('base/error');
//                }else{
//                    $order["data"]["status"] = 11;
//                }
//            }

//            if($order["data"]["status"]!=11){//非议价状态禁止编辑
//                $this->redirect('base/error');
//            }
            //print_r($order["data"]);exit();
            $this->assign('order', $order["data"]);
        }else{
            $this->redirect('base/error');
        }

        return view();
    }


    // 向维修店销售订单添加商品
    public function order_product_server_deal_add() {

        if (request()->isAjax()) {

            $order_id = input('param.order_id','0');
            $repair_id = input('param.repair_id','0');
            $product_id = input('param.product_id','');
            $product_name = input('param.product_name','');
            $product_num = input('param.product_num','');
            $product_price = input('param.product_price','');
            //$product_attr_id = input('param.product_attr_id','');
            $product_image = input('param.product_image','');
            $type = input('param.type','');
            $msg = input('param.msg','');
            $server_time = input('param.server_time','');
            $param = [
                "order_id" => $order_id,
                "repair_id" => $repair_id,
                "product_id" => $product_id,
                "product_name" => $product_name,
                "product_num" => $product_num,
                "product_price" => $product_price,
                //"product_attr_id" => $product_attr_id,
                "product_image" => $product_image,
                "type" => $type,
                "msg" => $msg,
                "server_time" => $server_time
            ];
            $data = call_interface('repair_order_product_server_deal_add',$param);
            return $data;

        }
        $this->redirect('base/error');

    }


    // 修改维修店销售订单商品的数量
    public function order_product_server_deal_update() {

        if (request()->isAjax()) {
            $id = input('param.id','0');
            $order_id = input('param.order_id','0');
            $product_num = input('param.product_num','0');
            $param = [
                "id" => $id,
                "order_id" => $order_id,
                "product_num" => $product_num
            ];
            $data = call_interface('repair_order_product_server_deal_update',$param);
            return $data;

        }
        $this->redirect('base/error');

    }


    // 删除维修店订单商品
    public function order_product_server_deal_delete() {

        if (request()->isAjax()) {

            $id = input('param.id','0');
            $order_id = input('param.order_id','0');
            $param = [
                "id" => $id,
                "order_id" => $order_id
            ];
            $data = call_interface('repair_order_product_server_deal_delete',$param);
            return $data;

        }
        $this->redirect('base/error');

    }

    // 确认推送维修店销售订单
    public function repair_deal_push() {

        if (request()->isAjax()) {

            $order_id = input('param.order_id','0');
            $repair_id = input('param.repair_id','0');
            $param = [
                "order_id" => $order_id,
                "repair_id" => $repair_id
            ];
            $data = call_interface('repair_deal_push',$param);
            return $data;

        }
        $this->redirect('base/error');

    }


}
